Newbie question: When to use extern "C" { //code } ?

Posted by Russel on Stack Overflow See other posts from Stack Overflow or by Russel
Published on 2010-05-09T06:11:06Z Indexed on 2010/05/09 6:18 UTC
Read the original article Hit count: 258

Filed under:
|
|

Hello,

Maybe I'm not understanding the differences between C and C++, but when and why do we need to use:

extern "C" {

? Apparently its a "linkage convention"?

I read about it briefly and noticed that all the .h header files included with MSVS surround their code with it. What type of code exactly is "C code" and NOT "C++ code"? I thought C++ included all C code?

I'm guessing that this is not the case and that C++ is different and that standard features/functions exist in one or the other but not both (ie: printf is C and cout is C++), but that C++ is backwards compatible though the extern "C" declaration. Is this correct?

My next question depends on the answer to the first, but I'll ask it here anyway: Since MSVS header files that are written in C are surrounded by extern "C" { ... }, when would you ever need to use this yourself in your own code? If your code is C code and you are trying to compile it in a C++ compiler, shouldn't it work without problem because all the standard h files you include will already have the extern "C" thing in them with the C++ compiler?

Do you have to use this when compiling in C++ but linking to alteady built C libraries or something?

Please help clarify this for me... Thanks!

--Keith

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++